<template>
  <el-container>
    <!--左侧-->
    <el-aside width="200px">
      <img src="../assets/loginBox.svg" />
      <p class="imgTitle">欢迎使用智慧云客系统</p>
    </el-aside>

    <!--右侧-->
    <el-main>
      <div class="loginTile">欢迎登录</div>

      <el-form
        ref="loginRefForm"
        :model="user"
        label-width="120px"
        :rules="loginRules"
      >
        <el-form-item label="账号" prop="loginAct">
          <el-input v-model="user.loginAct" />
        </el-form-item>

        <el-form-item label="密码" prop="loginPwd">
          <el-input type="password" v-model="user.loginPwd" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登 录</el-button>
        </el-form-item>

        <el-form-item>
          <el-checkbox label="记住我" v-model="user.rememberMe" />
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>


<script>
import { reactive, getCurrentInstance, onMounted } from "vue";
import { doGet, doPost } from "../http/httpRequest";
import { getTokenName, messageTip, removeToken } from "@/util/util";
export default {
  setup() {
    const { proxy } = getCurrentInstance();
    let user = reactive({});
    let loginRules = {
      loginAct: [
        { required: true, message: "请输入登录账号", trigger: "blur" },
      ],
      loginPwd: [
        { required: true, message: "请输入登录密码", trigger: "blur" },
        { min: 6, max: 16, message: "登陆密码长度为6-16位", trigger: "blur" },
      ],
    };
    function login() {
      proxy.$refs.loginRefForm.validate((isValid) => {
        if (isValid) {
          //验证通过，可以提交登录
          let formData = new FormData();
          formData.append("loginAct", user.loginAct);
          formData.append("loginPwd", user.loginPwd);
          formData.append("rememberMe", user.rememberMe);
          // formData.append("rememberMe", user.rememberMe);
          doPost("/api/login", formData).then((resp) => {
            console.log(resp);
            if (resp.data.code === 200) {
              messageTip("登录成功", "success");
              //删除历史token，sessionStroge和localStroge
              removeToken();
              if (user.rememberMe === true) {
                window.localStorage.setItem(getTokenName(), resp.data.data);
              } else {
                window.sessionStorage.setItem(getTokenName(), resp.data.data);
              }
              window.location.href = "/dashboard";
            } else {
              messageTip("登录失败", "fail");
            }
          });
        }
      });
    }
    onMounted(() => {
      freeLogin();
    });
    function freeLogin() {
      let token = window.localStorage.getItem(getTokenName());
      if (token) {
        doGet("/api/login/free", {}).then((resp) => {
          if (resp.data.code === 200) {
            window.location.href = "/dashboard";
          }
        });
      }
    }
    return { user, loginRules, login };
  },
};
</script>

<style scoped>
.el-aside {
  background: #1a1a1a;
  width: 40%;
  text-align: center;
}
.el-main {
  height: calc(100vh);
}
img {
  height: 413px;
}
.imgTitle {
  color: #f9f9f9;
  font-size: 28px;
}
.el-form {
  width: 60%;
  margin: auto;
}
.loginTile {
  text-align: center;
  margin-top: 100px;
  margin-bottom: 25px;
  font-weight: bold;
}
.el-button {
  width: 100%;
}
</style>